﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;
using Zhongchouwwwww.BAL;
using Zhongchouwwwww.Config;

namespace Zhongchouwwwww.webform
{
    public partial class Personal_settings : System.Web.UI.Page
    {
        private static int ThisUserID = Config.Config.ThisUser.UserId;
        private static ZhongchouDBEntities db = BAL.GetDate.db;
        private static string ThisUserSex = Config.Config.ThisUser.UserSex;
        private static string ThisUserName = Config.Config.ThisUser.UserName;
        private static string ThisUserDesc = Config.Config.ThisUser.Userdesc;
        private static DateTime ThisUserNikenametime = Config.Config.ThisUser.UserNikenametime;
        private static string ThisUserWebsite = Config.Config.ThisUser.UserWebsite;
        public static string ThisUserPhone = Config.Config.ThisUser.UserPhone;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BAL.DataVerification.IsSignin())
                Response.Redirect("user-login.aspx");

            #region 获取选中的标签并隐藏和显示
            string id = Request.QueryString["id"];
            if (id == null)
            {
                divzl.Attributes["style"] = "display:block";
                divshdz.Attributes.Add("style", "display:none");
                mmxg.Attributes.Add("style", "display:none");
            }
            switch (id)
            {
                case "1":
                    divzl.Attributes["style"] = "display:block";
                    divshdz.Attributes.Add("style", "display:none");
                    mmxg.Attributes.Add("style", "display: none");
                    break;
                case "2":
                    mmxg.Attributes["style"] = "display:block";
                    divshdz.Attributes.Add("style", "display:none");
                    divzl.Attributes.Add("style", "display:none");
                    break;
                case "3":
                    divzl.Attributes.Add("style", "display:none");
                    divshdz.Attributes.Add("style", "display:none");
                    mmxg.Attributes.Add("style", "display:none");
                    break;
                case "4":
                    divshdz.Attributes["style"] = "display:block";
                    divzl.Attributes.Add("style", "display:none");
                    mmxg.Attributes.Add("style", "display:none");
                    break;
                case "5":
                    divzl.Attributes.Add("style", "display:none");
                    divshdz.Attributes.Add("style", "display:none");
                    mmxg.Attributes.Add("style", "display:none");
                    break;
                default:
                    break;
            }
            #endregion


            

            if (!IsPostBack)
            {
                Name.Text = ThisUserName;
                if ("男".Equals(ThisUserSex))
                {
                    Sex.SelectedIndex = 0;
                }
                else if ("女".Equals(ThisUserSex))
                {
                    Sex.SelectedIndex = 1;
                }
                else if ("保密".Equals(ThisUserSex))
                {
                    Sex.SelectedIndex = 2;
                }
                userdesc.Text = ThisUserDesc;
                website.Text = ThisUserWebsite;
            }

        }
        #region 点击切换区域


        protected void unpwd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-settings.aspx?id=2");
        }

        protected void unpqw_Click(object sender, EventArgs e)
        {

        }

        protected void txxg_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-settings.aspx?id=3");
        }

        protected void shhdiz_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-settings.aspx?id=4");
        }

        protected void wdyhj_Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-settings.aspx?id=5");
        }

        protected void unpqw_7Click(object sender, EventArgs e)
        {
            Response.Redirect("Personal-settings.aspx?id=1");
        }
        #endregion

        protected void city1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ("广东".Equals(city1.SelectedValue))
            {
                city2.Items.Clear();
                city2.Items.Add("深圳");
                city2.Items.Add("广州");
                city2.Items.Add("惠州");
            }
            else if ("广西".Equals(city1.SelectedValue))
            {
                city2.Items.Clear();
                city2.Items.Add("南宁");
                city2.Items.Add("柳州");
                city2.Items.Add("北海");
            }
        }

        protected void SaveUsermsg_Click(object sender, EventArgs e)
        {
            bool IsTrue = true;
            DateTime now = DateTime.Now;
            DateTime last = ThisUserNikenametime;
            string NewuserName = Name.Text;
            if ("".Equals(city2.SelectedValue))
            {
                UserAddressMsg.Text = "请选择二级地区";
                return;
            }
           
            if (30 >= new TimeSpan(now.Ticks - last.Ticks).Days)
            {
                if (NewuserName == ThisUserName)
                {
                    IsTrue = true;
                }
                else
                {
                    UserNameMsg.Text = "你在30天内已经更改过一次名字了。";
                    Name.Text = ThisUserName;
                    return;
                }
            }
            if (IsTrue)
            {
                string sql = $"update Userinfo set usernikename='{NewuserName}',sex='{Sex.SelectedValue}',address='{city1.SelectedValue + city2.SelectedValue}',nikenametime='{now}',userdesc='{userdesc.Text}', website ='{website.Text}' where userid='{ThisUserID}'";
                if (GetDate.EXEXE(sql))
                {
                    Config.Config.ThisUser.UserName = NewuserName;
                    Config.Config.ThisUser.UserNikenametime = now;
                    Config.Config.ThisUser.UserSex = Sex.SelectedValue;
                    Config.Config.ThisUser.Userdesc = userdesc.Text; 
                    Response.Write("<script>alert('个人信息保存成功！')</script>");
                }
                else
                {
                    Response.Write("<script>alert('个人信息保存失败！')</script>");
                }
            }
            else
            {

            }
                
            
        }

        protected void AddressCity1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ("广东".Equals(AddressCity1.SelectedValue))
            {
                AddressCity2.Items.Clear();
                AddressCity2.Items.Add("深圳");
                AddressCity2.Items.Add("广州");
                AddressCity2.Items.Add("惠州");
            }
            else if ("广西".Equals(AddressCity1.SelectedValue))
            {
                AddressCity2.Items.Clear();
                AddressCity2.Items.Add("南宁");
                AddressCity2.Items.Add("柳州");
                AddressCity2.Items.Add("北海");
            }
        }

        protected void SaveAddmsg_Click(object sender, EventArgs e)
        {
           
            if ("".Equals(AddressCity2.SelectedValue))
            {
                Addaddmsg.Text = "请选择二级地区";
                return;
            }
            Addaddmsg.Text = "";
            if (!BAL.DataVerification.IsHandset(addphone.Text))
            {
                addphoneMsg.Text = "请输入正确的手机号码";
                return;
            }
            addphoneMsg.Text = "";
            if (!BAL.DataVerification.IsChinese(addname.Text))
            {
                addnameMsg.Text = "请输入正确真实姓名";
                return;
            }
            addnameMsg.Text = "";
            if (!BAL.DataVerification.IsPostNum(addpost.Text))
            {
                addpostmsg.Text = "请输入六位正确的邮编代码";
                return;
            }
            addpostmsg.Text = "";
            string sql = $"insert Addressinfo values('{addname.Text}','{addphone.Text}','{AddressCity1.SelectedValue + AddressCity2.SelectedValue}','{addaddress.Text}','{addpost.Text}','{ThisUserID}')";
            if (GetDate.EXEXE(sql))
            {
                Response.Write("<script>alert('收货地址保存成功！')</script>");
            }
            else
            {
                Response.Write("<script>alert('收货地址保存失败！')</script>");
            }
        }

        protected void Btnok_Click(object sender, EventArgs e)
        {
            if(OldPwd.Text != BAL.GetDate.db.Userinfo.Where(user=> user.userphone==Config.Config.ThisUser.UserPhone).FirstOrDefault().userpwd)
            {
                OldPwdMsg.Text = "原密码不正确，再仔细想想吧！";
                return;
            }
            OldPwdMsg.Text = "";
            if (NewPwd.Text != ReNewPwd.Text)
            {
                ReNewPwdMsg.Text = "新密码输入不一致！";
                return;
            }
            if (NewPwd.Text == OldPwd.Text)
            {
                ReNewPwdMsg.Text = "新密码与旧密码一致，再想一个吧！";
                return;
            }
            ReNewPwdMsg.Text = "";
            if (BAL.GetDate.EXEXE($"update Userinfo set userpwd = '{NewPwd.Text}' where userphone = '{Config.Config.ThisUser.UserPhone}'"))
            {
                Response.Write("<script>alert('密码修改成功！')</script>");
                Config.Config.ThisUser.UserId = 0;
                Config.Config.ThisUser.UserName = "";
            }
            else
            {
                Response.Write("<script>alert('密码修改失败，请联系管理员！')</script>");
            }
        }
    }
}